#include "EntityManager.h"
#include "Debug.h"

EntityManager::EntityManager(void)
{
	m_window = NULL;
}

EntityManager::~EntityManager(void)
{
	//delete entities...
}


void EntityManager::AddEntity( Entity* entity )
{
	m_entities.push_back(entity);
}

void EntityManager::RemoveEntity(Entity* entity)
{
	for (EntityList::iterator i = m_entities.begin(); i!=m_entities.end(); i++) {
		if (entity == *i) {
			delete *i;
			m_entities.erase(i);
			break;
		}
	}
}

void EntityManager::Update( uint16 diff )
{
	if(m_window == NULL) {
		sLog.outError("EntityManager::Update(): RenderWindow is NULL!");
		return;
	}

	for (EntityList::const_iterator i = m_entities.begin(); i!=m_entities.end(); i++) {
		Entity* current = *i;
		current->Update(diff);
	}

	sf::Sprite sprite;
	for (EntityList::const_iterator i = m_entities.begin(); i!=m_entities.end(); i++) {
		Entity* current = *i;
			
		const sf::Image* image = current->GetAnimation()->GetSpriteSheet();
		if(!image) continue;

		sprite.SetImage(*image);
		sprite.SetPosition((float)current->x(),(float)current->y());
		sprite.SetSubRect(current->GetAnimation()->GetRect());
		m_window->Draw(sprite);
	}
}